var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()
var Store = app.Store 

Page({
  data: {
    hasMore: true,// 判断 数据是否全部加载完
    isLodingLock: true, //锁 防止数据加载
    pageList: [], //分页数据列表
    pageNum: 1, //页码
    isShowKong: false, // 是否是空
    status:'', // 状态搜索 
    currentTab: 0, // 当前选中的tab
    nav: [
      { name: '一票', type: 0, num: 0, color: '#dd524d' },
      { name: '二票', type: 1, num: 0,  color: '#16304f'}
    ], // tab
    isShowTime: false , // 是否显示时间弹框
    isLose: false, // 打开忽略弹框
    isProcess:false,//打开审核流程弹框
    invalidReason: '', // 单
    steps:[
      {
        text: '工作负责人填写中',
      },
      {
        text: '工作负责人已提交',
      },
      {
        text: '签发人和值班人审核',
      },
      {
        text: '许可人压票中',
      },
      {
        text: '工作进行中',
      },
      {
        text: '工作票终结',
      }
    ],
    statusArr:['全部','填写中','已提交','审核中','审核通过','进行中','已完成','已作废'],
    statusIdx:0,
    userRole:''
  },

  onLoad: function (options) {
    console.log(Store.getState().main.userRoleList)
    this.setData({
      userRole: Store.getState().main.userRoleList.find((item)=>item=="work_work_leader")
    })
    this.getPagServer(1, true) //获取分页数据  
  },
  onShow: function () {
    this.getPagServer(1, true) //获取分页数据  
    api.$get(api.workTicket.ticketCount).then((res) => {
      var nav = this.data.nav
      nav[0].num = res.data.firstNumber
      nav[1].num = res.data.secondNumber
      this.setData({nav})
    })
  },
  selectStatus(e){
    this.setData({
      status: e.detail.value==0?'':e.detail.value-1,
      statusIdx:e.detail.value
    })
    this.getPagServer(1, true)
  },
  onReachBottom() { //滑动到底部触发
    this.loadingMore()
  },
  onPullDownRefresh: function () { // 下拉刷新
    this.getPagServer(1, true) //获取分页数据
  },


  loadingMore: function () {  // 加载更多
    console.log(this.data.isLodingLock, 111)
    if (this.data.isLodingLock) {
      this.setData({
        isLodingLock: false
      })

      this.getPagServer()
    }
  },

  getPagServer(pageNum, isClickTab = false) { //分页  
    var params = {
      pageNum: pageNum == 1 ? 1 : this.data.pageNum,
      pageSize: 10, 
      ticketType: this.data.currentTab+1,
      status: this.data.status 
    } 
    $.getPageList(api, this, api.workTicket.workTickList, params, isClickTab).then((res) => {
      wx.stopPullDownRefresh() // 接口数据请求完停止刷新
    })
  },

  //##input 输入##
  input_handle(e) {
    this.setData({
      [e.currentTarget.dataset.key]: e.detail.value
    })
  },
  //##input 输入##

  keywordConfirmHandle(){ // 搜索输入框 
    // if($.isNull(this.data.keyword)){
      // $.toast('请输入内容') 
    // }else {
      this.getPagServer(1, true) //获取分页数据
    // }

  },
  previewCurrent(e){
    var {item} = $.detail(e)
    if(!Boolean(item.imagePath)){
      $.toast('图片暂未生成，请联系管理员！')
      return false
    }
    // console.log(item.imagePath+`?a=${Math.random()*100}`)
    wx.previewImage({
      current: '', // 当前显示图片的链接，不填则默认为urls的第一张
      urls: [item.imagePath+`?a=${Math.random()*100}`] // 需要预览的图片链接列表
    })
  },
  
  alertProcess(e){
    var {item} = $.detail(e)
    this.setData({
      isProcess: true,
      active: item.status
    })
  },
  onClose(){
    this.setData({
      isProcess: false,
    })
  },
  tabHanlde(e) {
    var { index } = e.detail
    this.setData({
      currentTab: index
    })
    this.getPagServer(1, true) //获取分页数据
  },
  timeConfirmHandle(e){ // 时间组件传出来的数据
    var { time, key} = e.detail
    console.log(time, key)
  },
  openTimeHandle(e){ // 打开时间弹框
    this.setData({
      isShowTime: true
    })
  },
  openLoseHandle(e){ // 打开作废弹框
    var {item} = $.detail(e)
    this.setData({
      isLose: true,
      _id: item._id
    })
  },
  setZuoFei(){//作废
    if($.isNull(this.data.invalidReason)){
      $.toast("作废原因不能为空")
        return
    }
    api.$post(api.defect.setZuoFei, {}).then((res) => {
      $.toast('作废成功')
      setTimeout(() => {
        $.linkurl('', 'back')
      }, 1500)
    })
  },
  addRiskMng(e){
    var {item} = $.detail(e)
    $.linkurl(`/pages/workTicket/riskManage/riskManage?ticketInfo=${JSON.stringify(item)}`)
  },
  toChangeMember(e){
    const {item,type}=$.detail(e)
    $.linkurl(`/pages/workTicket/changeMember/changeMember?ticketInfo=${JSON.stringify(item)}&type=${type}`)
  },
  changeWorkTime(e){//添加开工手工时间
    const {item,type}=$.detail(e)
    $.linkurl(`/pages/workTicket/workTime/workTime?ticketNumber=${item.ticketNumber}&type=${type}`)
  },
  examWorkTime(e){//审核开工手工时间
    const {item,type}=$.detail(e)
    $.linkurl(`/pages/workTicket/workTimeExam/workTimeExam?ticketNumber=${item.ticketNumber}&type=${type}`)
  },
  memberExamList(e){
    const {item,type}=$.detail(e)
    $.linkurl(`/pages/workTicket/memberExam/memberExam?ticketNumber=${item.ticketNumber}&type=${type}`)
  },
  extension(e){
    const {item,type}=$.detail(e)
    $.linkurl(`/pages/workTicket/workExtension/workExtension?ticketNumber=${item.ticketNumber}&type=${type}`)
  },
  toWorkTmake(e){
    var that=this
    const {item,key}=$.detail(e)
    const rejectReason=null
    
    if(key=='btn'){
      if(that.data.currentTab==0){
        $.linkurl(`/pages/workTicket/workTicketMake/workTicketMake?ticketType=1&hasMesure=${false}`)
      }else{
        $.linkurl(`/pages/workTicket/workTicketMake/workTicketMake?ticketType=2&hasMesure=${false}`)
      }
    }else{
      if(Boolean(item.workLeaderComments))rejectReason=item.workLeaderComments
      if(Boolean(item.ticketIssuerComments))rejectReason=item.ticketIssuerComments
      if(Boolean(item.secondTicketIssuerComments))rejectReason=item.secondTicketIssuerComments
      if(Boolean(item.controlRoomOperatorComments))rejectReason=item.controlRoomOperatorComments
      if(Boolean(item.controlRoomLeaderComments))rejectReason=item.controlRoomLeaderComments
      if(Boolean(item.workPermitIssuerComments))rejectReason=item.workPermitIssuerComments
      wx.showModal({ 
        title: '被驳回原因',
        content: rejectReason||'流程不合规，无法完成,请修改',
        success (res) {
          if (res.confirm) {
            api.$get(api.workTicket.modifyTicketStatus, {ticketNumber:item.ticketNumber}).then((res) => {
              if(that.data.currentTab==0){ 
                $.linkurl(`/pages/workTicket/workTicketMake/workTicketMake?ticketNumber=${item.ticketNumber}&ticketType=1&hasMesure=${true}`)
              }else{
                $.linkurl(`/pages/workTicket/workTicketMake/workTicketMake?ticketNumber=${item.ticketNumber}&ticketType=2&hasMesure=${true}`)
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
    
    
    
  },
  toWorkTmake2(e){
    const {item,key}=$.detail(e)
    if(this.data.currentTab==0){ 
      $.linkurl(`/pages/workTicket/workTicketMake/workTicketMake?ticketNumber=${item.ticketNumber}&ticketType=1&hasMesure=${false}`)
    }else{
      $.linkurl(`/pages/workTicket/workTicketMake/workTicketMake?ticketNumber=${item.ticketNumber}&ticketType=2&hasMesure=${false}`)
    }
  },
  toWorkTicketPreview(e){ // 预览工作票
    var {item,type} = $.detail(e)
    console.log(type)
    $.linkurl(`/pages/workTicket/workTicketPreview/workTicketPreview?ticketNumber=${item.ticketNumber}&type=${type}`)
  },
  toOperationTicketMake(e){
    var {item} = $.detail(e)
    $.linkurl(`/pages/operationTicket/operationTicketMake/operationTicketMake?ticketInfo=${JSON.stringify(item)}&operationType=0`)
  },
  toDefectManageFlawRegister(e){ // 转缺陷
    var { item } = $.detail(e)
    item.source='告警'; 
    $.linkurl(`/pages/defectManage/defectManageFlawRegister/defectManageFlawRegister?info=${JSON.stringify(item)}&type=0`)
  },
})